<?php
namespace Manager\Controller;
use Think\Upload;
use Think\Page;
use Think\Image;

/**
* ==============================================
* URL：http://www.liuhuanhui.cn
* ==============================================
* @date：2014-11-13
* @time：下午12:53:48
* @author：liuhuanhui
* @encoding：UTF-8
*/
class PhotosController extends  AuthController//相册控制器
{
	/**
	 * 显示相册信息
	 */
	function  index()
	{
		$this->photos=D('PhotosortsView')->select();
		$this->display();
	}
	/**
	 * 删除相册信息
	 */
	function deletePhotosort()
	{
		if(I('id'))
		{
			if(M('photos')->where(array(photosort_id=>I('id')))->count()>0)
			{
					$this->success('请先删除该相册下的相片！');
			}else{
				if(M('photosort')->delete(I('id')))
				{
					$this->success('删除相册成功！');
				}else{
					$this->error('删除相册失败！');
				}
			}
		}
	}
	/**
	 * 上传图片
	 */
	function upload()
	{
		$this->photosort=M('photosort')->find(I('photosort'));
		$this->display('Photos_Uploads');
	}
	
	/**
	 * 上传图片处理
	 */
	function uploadHandle()
	{
		$path='/photo_images/'.$_GET['photosort_id'].'/';
		$upload=new Upload();
		$upload->savePath=$path;
		$upload->autoSub=false;
		$info=$upload->uploadOne($_FILES['fileList']);
		if ($info)
		{
			//生成缩略图
			$s = new \SaeStorage();
			$f = new \SaeFetchurl();
			$img= new \SaeImage();
			$first_img_pic= $info['url'];
			$img_data= $f->fetch( $first_img_pic);
			$img->setData($img_data);
			$img->resize(200);// 等比缩放
			$Smallimg= $img->exec(); // 执行处理并返回处理后的二进制数据
			$s->write('uploads',$path.'small_'.$info['savename'],$Smallimg, -1);
			
			if(!$info||!$s)
			{
				$this->error('照片Show上传失败！');
			}
		
			/*本地的
			$image = new Image();
			$image->open('uploads/'.$path.$info['savename']);// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
			$image->thumb(150, 150)->save('uploads/'.$path.'small_'.$info['savename']);
			*/
			$data=array(
					name=>$info['savename'],
					filename=>$info['savename'],
					size=>$info['savename'],
					date=>time(),
					user_id=>get_user_session(),
					photosort_id=>$_GET['photosort_id'],
			);
			
			if(M('photos')->add($data))
			{
				M('photosort')->where(array(id=>$_GET['photosort_id']))->SetInc('total');
				$this->ajaxReturn($info['savename']);
			}else
			{
				$this->ajaxReturn(2);		
			}
		}else{
			$this->ajaxReturn($upload->getError());
		}
	}
	/**
	 * 添加和编辑
	 */
	function create()
	{	
		$this->photosort=M('photosort')->find(I('id'));
		$this->display();
	}
	/**
	 * 添加用户和修改处理方法
	 */
	function createHandle()
	{
		$_POST['date']=time();
		if (I('id'))
		{
			if(M('photosort')->save($_POST))
			{
				$this->success('修改相册成功！');
			}else{
				$this->error('修改相册失败！');
			}

		}else{
			if(M('photosort')->add($_POST))
			{
				$this->success('创建相册成功！');
			}else{
				$this->error('创建相册失败！');
			}
		}	
	}
	/**
	 * 查看相册
	 */
	function view()
	{	
		$photos=M('photos');
		$where=array(photosort_id=>I('id'));
		$page=new Page($photos->where($where)->count(),48);
		$this->pageShow=$page->show();
		$this->images=$photos->where($where)->limit($page->firstRow,$page->listRows)->select();
		$where2['id']=array('neq',I('id'));
		$this->photosort=M('photosort')->field('name')->find(I('id'));
		$this->photosorts=M('photosort')->where($where2)->select();
		$this->display();
	}
	/**
	 * 删除指定图片
	 */
	function deleteImage()
	{
			$photo=M('photos');
			$p=$photo->field('filename,photosort_id')->find(I('id'));
			if($p)
			{
			//$st=new \saestorage();//使用sae删除
			//$myfile ='/photo_images/'.$p['photosort_id'].'/';
			//$result=$st->delete('uploads',$myfile.$p['filename']) ;
			//$result=$st->delete('uploads',$myfile.'small_'.$p['filename']) ;
			
			
			
			//本地删除
					$myfile ='uploads/photo_images/'.$p['photosort_id'].'/';//WINDOW
					$result=unlink($myfile.'small_'.$p['filename']);
					$result=unlink($myfile.$p['filename']);
					if($result)
					{
							if($photo->delete(I('id')))
							{
								$this->success('删除相片成功！');
							}else{
								$this->error('删除相片失败！');
								
							}
					}else{
						$this->error('删除相片失败！');
						
					}
			}
	}
	/**
	 * 编辑图片
	 */
	function imageedit()
	{
		if (I('id'))
		{
			$this->photo=M('photos')->find(I('id'));
			$this->display();
		}
	}
	 /* 
	 *//**
	 * 编辑图片处理
	 */
	function imageEditHandle()
	{
		if (I('id'))
		{
				if(M('photos')->save($_POST))
				{
					$this->success('修改图片信息成功！');
				}else{
					$this->error('修改图片信息失败！');
				}
		}
	}
	/**
	 * 设置封面图片
	 */
	function serCover()
	{
		if (I('id')&&I('sortId'))
		{
			$data=array(
			id=>I('sortId'),
			cover=>I('id'),
			);
				if(M('photosort')->save($data))
				{
					$this->success('设置图片封面成功！');
				}else{
					$this->error('设置图片封面失败！');
				}
		}
	}
	/**
	 * 移动文件
	 */
	function imageMove()
	{
			$photo=M('photos');
			$p=$photo->field('filename,photosort_id')->find(I('id'));
			if($p)
			{
				//$path='uploads/photo_images/';
				$path=C('[IMGS]');
				$myfile =$path.$p['photosort_id'].'/'.$p['filename'];
				$newPath=$path.I('sortId').'/';
				if(!file_exists($newPath))
				{
					mkdir($newPath);
				}
				$result=rename($myfile,$newPath.$p['filename']);
				if($result)
				{
						$data=array(id=>I('id'),photosort_id=>I('sortId'));
						if($photo->save($data))
						{
							$this->success('移动图片成功！');
						}else{
							$this->error('移动图片失败！');
						}
				}
			}
	}
}